﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BCGC.BLL.Helper;
using BCGC.BLL.BusinessObject;
using Jalal.Library.DataStructure;
using Jalal.Library.Utility;
using System.Collections.Generic;
using BCGC.BLL.BusinessObject.Resource;

public partial class Resources_Crack : PublicBasePage
{
    private int Id
    {
        get
        {
            if (ViewState["Id"] == null)
                ViewState["Id"] = 0;
            return (int)ViewState["Id"];
        }
        set { ViewState["Id"] = value; }
    }

    private int CurrentPageIndex
    {
        get
        {
            if (ViewState["CurrentPageIndex"] == null)
                ViewState["CurrentPageIndex"] = 1;
            return (int)ViewState["CurrentPageIndex"];
        }
        set { ViewState["CurrentPageIndex"] = value; }
    }

    private string SortOrder
    {
        get
        {
            if (ViewState["SortOrder"] == null)
                ViewState["SortOrder"] = "Title Desc";
            return (string)ViewState["SortOrder"];
        }
        set { ViewState["SortOrder"] = value; }
    }

    private int TotalData
    {
        get
        {
            if (ViewState["TotalData"] == null)
                ViewState["TotalData"] = 0;
            return (int)ViewState["TotalData"];
        }
        set { ViewState["TotalData"] = value; }
    }

    private int PageSize
    {
        get
        {
            if (ViewState["PageSize"] == null)
                ViewState["PageSize"] = 10;
            return (int)ViewState["PageSize"];
        }
        set { ViewState["PageSize"] = value; }
    }

    private void BindPager()
    {
        List<string> pages = new List<string>();
        int pageCount = (TotalData / PageSize) + ((TotalData % PageSize) > 0 ? 1 : 0);
        for (int i = 1; i <= pageCount; i++)
            pages.Add(i.ToString());

        rptPager.DataSource = pages;
        rptPager.DataBind();
    }

    protected void Page_Change(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "PagingCommand")
        {
            CurrentPageIndex = Convert.ToInt32(e.CommandArgument);
            LoadResources();
            BindPager();
        }
    }

    protected void Data_Sort(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "Sort")
        {
            SortOrder = (string)e.CommandArgument;
            LoadResources();
        }
    }

    protected void Search_Click(object sender, EventArgs e)
    {
        LoadResources();
        BindPager();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int id = Id = 0;
            if (!string.IsNullOrEmpty(Request.QueryString["id"]) && int.TryParse(Request.QueryString["id"], out id))
            {
                Id = id;
                LoadResources();
                BindPager();
            }
            else
                Response.Redirect(CustomSiteMap.Resources);
        }
    }

    private void LoadResources()
    {
        int totalData = 0;
        ReadWriteCustomCollection<Crack> cracks = Crack.GetCrackByGame(CurrentPageIndex, SortOrder, PageSize,
            string.IsNullOrEmpty(tbxTitle.Text) ? null : Security.BindStringInput(tbxTitle.Text, SecurityLevel.Three),
            Id, out totalData);

        TotalData = totalData;

        rptResources.DataSource = cracks;
        rptResources.DataBind();
    }

    protected void Pager_DataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            if (Convert.ToInt32(e.Item.DataItem) == CurrentPageIndex)
            {
                LinkButton lkbPageIndex = (LinkButton)e.Item.FindControl("lkbPageIndex");
                lkbPageIndex.CssClass = "page-index-selected";
            }
        }
    }
}
